package com.lhy.service;


import cn.hutool.core.util.IdUtil;
import com.lhy.pojo.Devices;
import com.lhy.pojo.Miner;
import com.lhy.pojo.User;
import com.mongodb.client.result.DeleteResult;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MinerService {

    @Resource
    private MongoTemplate mongoTemplate;

    //添加矿工信息到mongodb
    public void addMiner(Miner miner){
        //雪花算法生成矿工编号
        miner.setId(IdUtil.getSnowflake(1,1).nextIdStr());
        //更新时间
        miner.setUpdateTime(new Date());
        //添加矿工信息到mongodb
        mongoTemplate.insert(miner,"miner");
    }

    //通过矿工id查询
    public Miner selectMinerById(String id){
        Query query = new Query(Criteria.where("_id").is(id));
        return mongoTemplate.findOne(query,Miner.class,"miner");
    }

    //通过矿工姓名查询
    public Miner selectMinerByName(String name){
        Query query = new Query(Criteria.where("name").is(name));
        return mongoTemplate.findOne(query,Miner.class,"miner");
    }

    //查询所有矿工
    public Map<String,Object> selectMinerList(){
        List<Miner> list = mongoTemplate.findAll(Miner.class,"miner");
        Map<String,Object> result =new HashMap<>();
        if(list==null || list.isEmpty()){
            result.put("code","400");
            result.put("message","没有相关信息");
        }else{
            result.put("code","0");
            result.put("count",list.size());
            result.put("data",list);
        }

        return result;
    }

    public Map<String,Object> selectMinerListOn(){
        List<Miner> list = mongoTemplate.find(new Query(Criteria.where("status").is("在职")),Miner.class,"miner");
        Map<String,Object> result =new HashMap<>();
        if(list==null || list.isEmpty()){
            result.put("code","400");
            result.put("message","没有相关信息");
        }else{
            result.put("code","0");
            result.put("count",list.size());
            result.put("data",list);
        }

        return result;
    }


    //根据工号删除
    public boolean deleteMinerById(String id){
        Query query = new Query(Criteria.where("_id").is(id));
        DeleteResult result = mongoTemplate.remove(query, Miner.class, "miner");
        return result.getDeletedCount() >0 ? true : false;
    }

    //根据工号修改矿工信息
    public void updateMinerInfo(Miner miner){
        Miner preMiner = selectMinerById(miner.getId());
        Query query = new Query(Criteria.where("_id").is(miner.getId()));
        Boolean exists = mongoTemplate.exists(query, Miner.class, "miner");
        if(exists==false) return;
        Update update=new Update();
        miner.setUpdateTime(new Date());

        if(miner.getName()!="") update.set("name",miner.getName());

        if(miner.getName()!="") update.set("name",miner.getName());

        if(miner.getSex()!="") update.set("sex",miner.getSex());

        if(miner.getPhone()!="") update.set("phone",miner.getPhone());

        if(miner.getAddress()!="") update.set("address",miner.getAddress());

        if(miner.getPosition()!="") update.set("position",miner.getPosition());

        if(miner.getJob()!="") update.set("job",miner.getJob());

        if(miner.getStatus()!="") update.set("status",miner.getStatus());

        mongoTemplate.upsert(query,update,Miner.class,"miner");
    }


}
